//
//  FilterViewController.swift
//  FeastBooking
//
//  Created by Imon on 2017/12/2.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

class FilterViewController: UIViewController {

    var completion: ((String, String, String) -> Void)?
    
    var minRating: String = ""
    var minPrice: String = ""
    var maxPrice: String = ""
    
    let gradingView = GradingView()
    
    let ratingTitleView: FilterTitleView = {
        let titleView = FilterTitleView()
        titleView.label.text = LanguageHelper.getString(key: "RATING_RANGE")
        return titleView
    }()
    
    let priceTitleView: FilterTitleView = {
        let titleView = FilterTitleView()
        titleView.label.text = LanguageHelper.getString(key: "PRICE_RANGE")
        return titleView
    }()
    
    let minPriceLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 22)
        label.text = "$"
        return label
    }()
    
    let minPriceField: UITextField = {
        let textfield = UITextField()
        textfield.font = AppFont(size: 20)
        textfield.tintColor = UIColor.black
        textfield.textColor = UIColor.black
        textfield.keyboardType = .decimalPad
        textfield.placeholder = LanguageHelper.getString(key: "MIN_PRICE")
        textfield.layer.borderWidth = 1
        textfield.layer.borderColor = UIColor.gray.cgColor
        textfield.layer.cornerRadius = 8
        textfield.layer.masksToBounds = true
        textfield.textAlignment = .center
        return textfield
    }()
    
    let toLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 20)
        label.text = "——"
        return label
    }()
    
    let maxPriceLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 22)
        label.text = "$"
        return label
    }()
    
    let maxPriceField: UITextField = {
        let textfield = UITextField()
        textfield.font = AppFont(size: 20)
        textfield.tintColor = UIColor.black
        textfield.textColor = UIColor.black
        textfield.keyboardType = .decimalPad
        textfield.placeholder = LanguageHelper.getString(key: "MAX_PRICE")
        textfield.layer.borderWidth = 1
        textfield.layer.borderColor = UIColor.gray.cgColor
        textfield.layer.cornerRadius = 8
        textfield.layer.masksToBounds = true
        textfield.textAlignment = .center
        return textfield
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = LanguageHelper.getString(key: "FILTER")
        setBackBarItem()        
        
        let rightBt = UIButton()
        rightBt.contentHorizontalAlignment = .right
        rightBt.setTitle(LanguageHelper.getString(key: "CONFIRM"), for: .normal)
        rightBt.setTitleColor(doneItemButtonColor, for: .normal)
        rightBt.addTarget(self, action: #selector(submit(_:)), for: .touchUpInside)
        rightBt.sizeToFit()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: rightBt)
        
        ratingTitleView.frame = CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 30)
        
        gradingView.frame = CGRect(x: 25, y: ratingTitleView.frame.origin.y + ratingTitleView.frame.height + 20, width: self.view.bounds.width - 50, height: (self.view.bounds.width - 50) / 6)
        
        priceTitleView.frame = CGRect(x: 0, y: gradingView.frame.origin.y + gradingView.frame.height + 20, width: self.view.bounds.width, height: 30)
        
        let toLabelSize: CGFloat = 18
        let labelSize: CGFloat = 20
        let fieldSize: CGFloat = 34
        let leading: CGFloat = self.view.bounds.width / 15
        minPriceLabel.frame = CGRect(x: leading, y: priceTitleView.frame.origin.y + priceTitleView.frame.height + 40, width: labelSize, height: labelSize)
        minPriceField.frame = CGRect(x: minPriceLabel.frame.origin.x + minPriceLabel.frame.width, y: minPriceLabel.frame.origin.y - (fieldSize - labelSize) / 2, width: self.view.bounds.width / 3.5, height: fieldSize)
        
        toLabel.frame = CGRect(x: (self.view.bounds.width - toLabelSize) / 2, y: priceTitleView.frame.origin.y + priceTitleView.frame.height + 40, width: toLabelSize, height: toLabelSize)
        maxPriceField.frame = CGRect(x: self.view.bounds.width - self.view.bounds.width / 3.5 - leading, y: priceTitleView.frame.origin.y + priceTitleView.frame.height + 40 - (fieldSize - labelSize) / 2, width: self.view.bounds.width / 3.5, height: fieldSize)
        maxPriceLabel.frame = CGRect(x: maxPriceField.frame.origin.x - labelSize, y: priceTitleView.frame.origin.y + priceTitleView.frame.height + 40, width: labelSize, height: labelSize)
        self.view.addSubview(ratingTitleView)
        self.view.addSubview(gradingView)
        self.view.addSubview(priceTitleView)
        self.view.addSubview(minPriceLabel)
        self.view.addSubview(minPriceField)
        self.view.addSubview(toLabel)
        self.view.addSubview(maxPriceLabel)
        self.view.addSubview(maxPriceField)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func submit(_ sender: UIButton) {
        minRating = "\(gradingView.grade)"
        minPrice = minPriceField.text!
        maxPrice = maxPriceField.text!
        if let completion = self.completion {
            completion(minRating, minPrice, maxPrice)
        }
        self.navigationController?.popViewController(animated: true)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let point = touch.location(in: self.view)
            if !minPriceField.frame.contains(point) {
                minPriceField.resignFirstResponder()
            }
            if !maxPriceField.frame.contains(point) {
                maxPriceField.resignFirstResponder()
            }
        }
    }
}
